##===----------------------------------------------------------------------===##
#
# Copyright (C) Intel Corporation
#
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
# This file incorporates work covered by the following copyright and permission
# notice:
#
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
#
##===----------------------------------------------------------------------===##

# Installation path: <onedpl_root>/lib/cmake/oneDPL/
get_filename_component(_onedpl_root "${CMAKE_CURRENT_LIST_DIR}" REALPATH)
get_filename_component(_onedpl_root "${_onedpl_root}/../../../" ABSOLUTE)

if (WIN32)
    set(_onedpl_headers_subdir windows)
else()
    set(_onedpl_headers_subdir linux)
endif()

get_filename_component(_onedpl_headers "${_onedpl_root}/${_onedpl_headers_subdir}/include" ABSOLUTE)

if (EXISTS "${_onedpl_headers}")
    if (NOT TARGET oneDPL)
        add_library(oneDPL INTERFACE IMPORTED)
        set_target_properties(oneDPL PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${_onedpl_headers}")

        # Handle oneTBB backend
        find_package(TBB 2021 QUIET COMPONENTS tbb)
        if (NOT TBB_FOUND)
            message(STATUS "oneDPL: oneTBB is not found, set ONEDPL_USE_TBB_BACKEND=0")
            set_property(TARGET oneDPL APPEND PROPERTY INTERFACE_COMPILE_DEFINITIONS ONEDPL_USE_TBB_BACKEND=0)
        else()
            set_target_properties(oneDPL PROPERTIES INTERFACE_LINK_LIBRARIES TBB::tbb)
        endif()

        # Handle DPC++ backend
        include(CheckCXXCompilerFlag)
        check_cxx_compiler_flag("-fsycl" _fsycl_option)
        if (NOT _fsycl_option)
            message(STATUS "oneDPL: -fsycl is not supported by current compiler, set ONEDPL_USE_DPCPP_BACKEND=0")
            set_property(TARGET oneDPL APPEND PROPERTY INTERFACE_COMPILE_DEFINITIONS ONEDPL_USE_DPCPP_BACKEND=0)
        endif()
    endif()
else()
    message(STATUS "oneDPL: headers do not exist ${_onedpl_headers}")
    set(oneDPL_FOUND FALSE)
endif()
